package com.ihd.ihardware.view.enter.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.widget.SwipeItemLayout;
import com.ihd.ihardware.databinding.ActivityLockManageBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.view.enter.viewModel.DevicesViewModel;
import com.ihd.ihardware.view.lock.view.LockBindingActivity;

import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;
import cn.wowjoy.commonlibrary.widget.titlebar.TitleBar;

public class LockManageActivity extends BaseActivity<ActivityLockManageBinding, DevicesViewModel> {
    private List<DeviceListRes.DataBean.ListBean> ddls;
    private MDialog alert;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        ddls = (List<DeviceListRes.DataBean.ListBean>) getIntent().getSerializableExtra("devices");
        binding.mtitlebar.setLeftImageResource(R.drawable.back);
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setTitle("蓝牙锁");
        binding.mtitlebar.addAction(new TitleBar.Action() {
            @Override
            public String getText() {
                return null;
            }

            @Override
            public int getDrawable() {
                return R.drawable.text_nb_add;
            }

            @Override
            public void performAction(View view) {
                Intent mIntent = new Intent(LockManageActivity.this, LockBindingActivity.class);
                startActivity(mIntent);
            }
        });

        setRx(AppConstans.UNBIND, new BaseConsumer<DeviceListRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(DeviceListRes mLiveData) {
                DialogUtils.successDialog(LockManageActivity.this, MDialog.DG_TYPE.SUCCESS, R.drawable.toasticon_suc, "解除成功");
                ddls.clear();
                SPUtils.putString(AppConstans.DEVICES, new Gson().toJson(mLiveData.getData().getList(), new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
                }.getType()));
                for (DeviceListRes.DataBean.ListBean d : mLiveData.getData().getList()) {
                    if ("2".equals(d.getType()) || "3".equals(d.getType()))
                        ddls.add(d);
                }
                viewModel.setMembers(ddls);
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(LockManageActivity.this, error.getMessage());
            }
        });

        binding.lockRV.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        binding.lockRV.setAdapter(viewModel.mZWAdapter);
        viewModel.setMembers(ddls);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lock_manage;
    }

    @Override
    protected Class getViewModelClass() {
        return DevicesViewModel.class;
    }

}
